<?php
class NewAction extends CommonAction{

	public function  index(){
		import('ORG.Util.Page');

		$count=M('news')->count();// 查询总数据记录

		$Page = new Page($count,C('PAGESIZE'));

		$list = M('news')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

		$show= $Page->show();
		
		$this->assign("list",$list);

		$this->assign("page",$show);

		$this->display();

	}

	
	public function add(){
		if(isset($_POST['submit'])){
			$data['title'] = $_POST['title'];
			$data['content'] = $_POST['content'];
			$data['createtime'] =mktime();
			if(M('news')->add($data)){

                if ($_POST['xzh_post'] == 1){
                    $this->success("添加成功！并自动推送到熊掌号资源提交",U('index'));
                } elseif ($_POST['xzh_post'] == 2){
                    $this->success("添加成功！并将【原创】自动推送到熊掌号资源提交",U('index'));
                }else{
                    $this->success("添加成功！",U('index'));
                }
			}else{
				$this->error("添加失败！",U('index'));
			}
		}else{
			$this->display();
		}
	}
	
	
	public function edit(){
		$id = empty($_GET['id'])?exit():$_GET['id'];
		$list = M('news')->where("id=".$id)->find();
		if(isset($_POST['submit'])){
			$data['title'] = $_POST['title'];
			$data['content'] = $_POST['content'];
			$data['createtime'] =mktime();
			M('news')->where('id='.$id)->save($data);
			$this->success("修改成功！",U('index'));
		
			
		}else{
			$this->assign("list",$list);
			$this->display();
		}
	
	}

	public function del(){
		 $qstr = $_GET['qstr'];
  		  	$data=M('news');  		  
			if(isset($qstr)){
			   $arr=explode(",",$qstr);

			   foreach($arr as $val){

			     $data->where("id=$val")->delete();
			       
			   }
			   $this->success("删除成功！",U('index'));			

			}else{
		$id = empty($_GET['id'])?exit():$_GET['id'];
		if(isset($id)){
			M('news')->where('id='.$id)->delete();
			$this->success("删除成功！",U('index'));
		}else{
			$this->error("删除失败！",U('index'));
		}
			}
		
	}
	
}
?>
